package com.eliansun.crawler.pageparse.jiangxi;

import com.eliansun.crawler.pageparse.AbstractPageProcess;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * user: lxc
 * data:2019- 07-03-20
 * 金利达药品交易网: www.123ypw.com
 */
public class Jinlida extends AbstractPageProcess {
    int count = 0;

    @Override
    public void process(Page page) {

        Map<String, Object> resMap = new HashMap<>();

        //列表界面
        if (page.getUrl().toString().indexOf("product_list") != -1) {
            List<Selectable> nodes = page.getHtml().xpath("//div[@class=div-table]/table/tbody//tr[@class=suger-list]/td[6]/a").nodes();
            if (nodes.size() == 0) {
                return;
            }

            //列表界面解析出对应的详情url，放到队列中
            for (Selectable node : nodes) {
                String s = node.links().toString();
                page.addTargetRequest(s);
            }

            //获取下一页放到队列中
            Object getTotalPage = page.getHtml().xpath("//span[@class=paging-info]/span[2]/text()");
            if (getTotalPage != null) {
                //总页数
                Integer totalPage = Integer.parseInt(getTotalPage.toString());
                for (int i = 1; i < totalPage; i++) {
                    String url = page.getUrl().toString();
                    page.addTargetRequest(url.replace("currentPage=1", "currentPage=" + (i + 1)));
                }
            }
        } else {
            //详情界面

            // 商品名称
            resMap.put("goodsName", page.getHtml().xpath("//div[@class=mt15]/div[1]//div[@class=b]/text()").get());

            //生产企业
            resMap.put("sccj", page.getHtml().xpath("//div[@class=mt15]/div[3]//div[@class=b]/text()").get());
            String medicalDeviceNo = page.getHtml().xpath("//div[@class=mt15]/div[4]//div[@class=b]/text()").toString();


            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("spec", "");
            resultMap.put("medicalDeviceNo", medicalDeviceNo);
            resultMap.put("productCompany", resMap.get("sccj"));
            resultMap.put("price", null);
            resultMap.put("brand", "");
            resultMap.put("productName", resMap.get("goodsName"));
            resultMap.put("executiveStandard", "");
            resultMap.put("nationality", "");
            resultMap.put("store", super.cTerrace);
            resultMap.put("url", page.getUrl());
            resultMap.put("version", "");
            page.putField("result", resultMap);
            count++;
            System.out.println(count);
        }
    }

    Site site = Site.me()
            //设置编码
            .setCharset("utf-8")
            //设置抓取间隔时间
            .setSleepTime(1000 * 3)
            //设置超时时间，单位毫秒
            .setTimeOut(1000 * 3)
            //设置重试间隔时间
            .setRetrySleepTime(1000 * 3)
            //设置重试次数
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }


    public static void main(String[] args) {
        Spider.create(new Jinlida())
                // https://www.123ypw.com/product_list.html?gn=%E5%A4%B4%E5%AD%A2%E6%8B%89%E5%AE%9A&fot=&ccd=&gtn=&spec=&brand=&isbm=&maf=&currentPage=1
                // https://www.123ypw.com/product/CM1000037527.html
                .addUrl("https://www.123ypw.com/product_list.html?gn=@key@&fot=&ccd=&gtn=&spec=&brand=&isbm=&maf=&currentPage=1")
                //设置多个线程同时抓取数据
                .thread(10)
                .run();

    }
}
